	
	var projectIndex=1;
	
	var taskIndex=1;
	
	function addProject(){
		var projectTr=$('#projectTr').clone(true);
		$(projectTr).removeAttr("id");
		$(projectTr).find(".dynamic").each(function(i){
			if($(this).attr("projectIndex")!=null){
				$(this).attr("projectIndex",projectIndex);
			}
		});
		$(projectTr).find("#project_name_")[0].setAttribute("id","project_name_"+projectIndex);
		$(projectTr).find("#task_list_")[0].setAttribute("id","task_list_"+projectIndex);
		$("#mainTable").append(projectTr);
		
		$("#mainTable").append($('#spacingTr').clone(true));
		
		projectIndex++;
		
		return projectTr;
	}
	
	function addTask(projectIndex){
		taskIndex++;
		var taskTr=$('#taskTr').clone(true);
		$(taskTr).attr("id","taskTr_"+projectIndex+"_"+(taskIndex));
		$(taskTr).find("#task_name_")[0].setAttribute("id","task_name_"+taskIndex);
		$(taskTr).find("#terminal_info_")[0].setAttribute("id","terminal_info_"+taskIndex);
		$(taskTr).find("#memoTip_")[0].setAttribute("id","memoTip_"+taskIndex);
		
	
		var starTime=$(taskTr).find("#startTime");
		var endTime=$(taskTr).find("#endTime");
		starTime.attr("id","startTime_"+projectIndex+"_"+taskIndex);
		endTime.attr("id","endTime_"+projectIndex+"_"+taskIndex);
		
		var cooperateUser=$(taskTr).find("#cooperateUser");
		cooperateUser.bind("click",function(){
			chooseCooperateUser(this);
		});
		
		starTime.bind("click",function(){
			WdatePicker({dateFmt:'HH:mm:ss',maxDate:'#F{$dp.$D(\''+endTime.attr("id")+'\')}'})
		});
		endTime.bind("click",function(){
			WdatePicker({dateFmt:'HH:mm:ss',minDate:'#F{$dp.$D(\''+starTime.attr("id")+'\')}'})
		});
		
		$(taskTr).find(".dynamic").each(function(i){
			if($(this).attr("taskIndex")!=null){
				$(this).attr("taskIndex",taskIndex);
			}
			if($(this).attr("projectIndex")!=null){
				$(this).attr("projectIndex",projectIndex);
			}
		});
		
		$("#task_list_"+projectIndex).append(taskTr);
		
		showTaskNum(projectIndex);
		
		return taskTr;
	}
	
	function delTask(delBtn){
		//if(!confirm('删除任务?')){
		//	return;
		//}
		var trId='taskTr_'+$(delBtn).attr('projectIndex')+"_"+$(delBtn).attr('taskIndex')
		$("#"+trId).remove();
		
		showTaskNum($(delBtn).attr('projectIndex'));
	}
	
	function showTaskNum(projectIndex){
		var i=1;
		$(".taskNum").each(function(){
			if($(this).attr('projectIndex')==projectIndex){
				$(this).html('任务'+(i++));
			}			
		});
	}
	
	function doCancel() {
		document.getElementById("planForm").action = "<c:url value='/planMain.html' />";	
		document.getElementById("planForm").submit();
	}
	
	
	function chooseProject(projectIndex){
		var a=window.showModalDialog('../project/projectbrowserList.html',window,'status:no;resizable:no;directories:no;help:no;scroll:no;dialogWidth=660px;dialogHeight=360px;dialogLeft:500px;dialogTop:300px;');
        var projectId=a.projectId;
        var projectName=a.projectName;
        var projectCode=a.projectCode;
        $('#project_name_'+projectIndex).val(projectName);
        $('#project_name_'+projectIndex).attr('projectId',projectCode);
        $('#project_name_'+projectIndex).attr('projectCode',projectId);
	}
	
	
	function chooseChannel(taskIndex,projectIndex){
		var url="../channelList.html";
		
		if($("#project_name_"+projectIndex).val() && $("#project_name_"+projectIndex).val()!=''){
			url+="?projectId="+$("#project_name_"+projectIndex).attr("projectCode");
			if($("#empId").val()!=''){
				url+="&empId="+$("#empId").val();
			}
		}else{
			alert("请先选择项目");
			return;
		}
		var a=window.showModalDialog(url,window,'status:no;resizable:no;directories:no;help:no;scroll:no;dialogWidth=800px;dialogHeight=450px;dialogLeft:500px;dialogTop:300px;');
		$('#terminal_info_'+taskIndex).val(a[1]);
		$('#terminal_info_'+taskIndex).attr("channelAddress",a[2]);
		$('#terminal_info_'+taskIndex).attr("channelCode",a[0]);
		
		$('#memoTip_'+taskIndex).unbind("click");
		$('#memoTip_'+taskIndex).bind("click",function(){
			openMemoTip(a[0],this);
		});
		
	}
	
	function chooseTask(taskIndex,projectIndex){
		
		if($("#project_name_"+projectIndex).val()==''){
			alert("请先选择项目");
			return;
		}
		
		var projectId=$("#project_name_"+projectIndex).attr("projectCode");
		
		var a=window.showModalDialog('../admin/jobList.html?projectId='+projectId,window,'status:no;resizable:no;directories:no;help:no;scroll:no;dialogWidth=800px;dialogHeight=450px;dialogLeft:500px;dialogTop:300px;');
		$('#task_name_'+taskIndex).val(a.jobName);
		$('#task_name_'+taskIndex).attr("jobId",a.jobId);
	}
	
	function jobListOnloadCallBack(subWindow){
		var subDocument=subWindow.document;
		$(subDocument).find('.delBtn').each(function(i){
			$(this).hide();
		});
		$(subDocument).find('.chooseBtn').each(function(i){
			$(this).show();
			$(this).bind("click",function(){
				subWindow.parent.window.returnValue={jobName:$(this).attr("jobName"),jobId:$(this).attr("jobId")};
				subWindow.close();
			});
		});
		
		$(subDocument).find('.editBtn').each(function(i){
			$(this).hide();
		});
	}
	
	function checkNull(){
		assignNameForNull();
		return $("#planForm").valid() && (projectIndex>1);	
	}
	
	/**
	 *
	 */
	function submitCreatePlan(){
		if(!checkNull()){
			return;
		}
		
		
		var planProjects=new Array();
		for(var i=1;i<projectIndex;i++){
			var planJobs=findPlanJobs(i);
			
			var projectCode=$("#project_name_"+i).attr("projectid");
			var projectName=$("#project_name_"+i).val();
			
			planProjects.push(new PlanProject({
				projectCode : projectCode,
        		projectName : projectName,
        		planJobs:planJobs
			}));
		}
		
		var plan=new Plan({
			 planDate:$('#planDate').val(),
			 status:PlanStatus.NEW,
			 name:$('#planName').val(),
			 planProjects:planProjects
		});
		
		if($("#planId").val()!=""){
			plan.id=$("#planId").val();
			plan.version=$("#planVersion").val();
			
		}
		
		if($("#userLoginName").val()!=""){
			plan.userLoginName=$("#userLoginName").val();
		}
		
		if($("#planType").val()!=""){
			plan.type=$("#planType").val();
		}
		
		if($("#assignedBy").val()!=""){
			plan.assignedBy=$("#assignedBy").val();
		}
		
//		alert(JSON.stringify(plan));
//		return;
		
		$.ajax(
    		{
		    	type :'POST',
		    	url : 'newPlanProject.html', 
		        data: JSON.stringify(plan),   
		        dataType: 'text',  
		        contentType:'application/json;charset=UTF-8',
		        success:function(data){
		        	var jsonData=JSON.parse(data);
		        	if(jsonData.success){
		        		alert('计划保存成功!');
		        		if($("#searchType").val()=='subordinate'){
		        			window.location='../plan/planList.html?from=subordinate';
		        		}else{
		        			window.location='../plan/planList.html';
		        		}
		        	}else{
		        		alert("计划保存失败  -- "+jsonData.msgBody);
		        	}
		        }
    		}
        );   
		
	}
	
	
	function findPlanJobs(projectIndex){
		var planJobs=new Array();
		
		
		$("#task_list_"+projectIndex).children().each(function(i){
			
			var channelElement=$(this).find("input[id^='terminal_info_']")[0];
			var jobElement=$(this).find("input[id^='task_name_']")[0];
			
			
			var storeCode=channelElement.getAttribute("channelcode");
		    var storeName=channelElement.value;
		    var storeAddress=channelElement.getAttribute("channeladdress");
		    var jobId=jobElement.getAttribute("jobid");
		    
		    var startTime=$(this).find("input[id^='startTime']").val();
		    var endTime=$(this).find("input[id^='endTime']").val();
		    
		    var cooperateUser=$(this).find("input[id^='cooperateUser']").attr("loginName");
		    
		    
			planJobs.push(new PlanJob({
				storeCode : storeCode,
        		storeName : storeName,
        		storeAddress : storeAddress,
        		jobId : jobId,
        		startTime : startTime,
        		endTime : endTime,
        		cooperateUser:cooperateUser
			}));
			
			
		});
		return planJobs;
	}
	
	
	
	
	
	function openMemoTip(storeId,obj){
		
		$.getJSON("findByUserIdAndChannelCodeOrderByRecordDate.html?channelCode="+storeId, function(jsonData){
			
			if(jsonData.length<1){
				alert("当前终端没有备忘");
				return false;
			}
			
			var lis="";
			for(var j in jsonData){
				lis+="<li>"+jsonData[j].content+"</li>";
			}
			
			var tooltip = "<div class='globalText' style='min-height:150px;display:none;position: absolute;background-color: #f1f1f1;border: solid 1px #999999;padding:1px;' id='memoTip'>"+lis+"</div>";
			
			
			if(!document.getElementById('memoTip')){
				$('body').append($(tooltip));
			}
			
			
			
			var position = $(obj).position();
			$('#memoTip').css({"opacity" : "0.9",
							       "top" : (position.top) + "px",
								  "left" : (position.left+30) + "px"
							  }).show('fast');
							  
							  
							  
			$('body').bind("click",function(e){
				if(e.target.getAttribute("id")!='memoTip' && e.target!=obj){
					$('#memoTip').fadeOut();
					$('body').unbind("click");
				}
			});		
		
		});	
	}
	
	function chooseCooperateUser(obj){
			
			if($("#project_name_"+$(obj).attr('projectIndex')).val()==''){
				alert("请先选择项目");
				return;
			}
			
			var projectCode=$("#project_name_"+$(obj).attr('projectIndex')).attr("projectId");
			
			
			if(!employeeUser[projectCode]){
				alert("该项目下没有下属");
				return;
			}
			
			var emps=employeeUser[projectCode];
			
			var lis="";
			
			for(var i=0;i<emps.length;i++){
				lis+="<li id='liEle' style='cursor: pointer;' loginName='"+emps[i].loginName+"'>"+emps[i].displayName+"</li>";
			}
			
		
			var cooperateTip = "<div class='globalText' style='min-height:150px;min-width:100px;display:none;position: absolute;background-color: #f1f1f1;border: solid 1px #999999;padding:8px;' id='cooperateTip'>"+lis+"</div>";
			
			
			if(!document.getElementById('cooperateTip')){
				$('body').append($(cooperateTip));
			}
			
			var position = $(obj).position();
			$('#cooperateTip').css({"opacity" : "0.9",
							       "top" : (position.top) + "px",
								  "left" : (position.left+30) + "px"
							  }).show('fast');
							  
			$('body').bind("click",function(e){
				if(e.target.getAttribute("id")=="liEle"){
					$(obj).val($(e.target).html());
					$(obj).attr("loginName",$(e.target).attr("loginName"));
					$('#cooperateTip').fadeOut();
				}
				if(e.target.getAttribute("id")!='cooperateTip' && e.target!=obj && e.target.getAttribute("id")!='liEle'){
					$('#cooperateTip').fadeOut();
					$('body').unbind("click");
				}
			});		
	}
	